﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel; 
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil.CustomObject;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Numeric
{
    [Serializable]
    [VerfityAttribute(Id = "VerfityD0003", Name = "逻辑检证", CategoryType = EnumCategoryType.数值, Model = true)]
    public class VerfityD0003 : VerfityOneResult
    {
        private CustomCollection<Conditinon> _condition = new CustomCollection<Conditinon>() { Description = "" };


        [TypeConverter(typeof(CustomCollectionConverter<Conditinon>))]
        [RefreshProperties(RefreshProperties.All)]
        public CustomCollection<Conditinon> Conditinons
        {
            get { return _condition; }
            //set { _condition = value; }
        }

        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;

            if (entryCommon == "30"  ||
                 entryCommon == "20")
            {
                return false;
            }

            //循环每行
            for (int i = 0; i < result.Count; i++)
            {
                var rowResult = result[i];

                for (int j = 0; j < rowResult.Count; j++)
                {
                    var colResult = rowResult[j];

                    bresult = CheckResult(i,j,colResult);
                    if (bresult)
                        break;

                }

                if (bresult)
                    break;
            }


            if (bresult)
            {
                System.Console.WriteLine("VerfityD0003"); 
            }
            return bresult;
        }


        private bool CheckResult(int row,int col,string result)
        {
            bool bresult = false;

            foreach (var condition in Conditinons)
            {
                //precondition = null
                if (condition.PreCnditions.ResultLength ==0 && condition.PreCnditions.Startindex  == -1)
                {
                    foreach (var elementSetting in condition.ElementSettings)
                    {
                        bresult = !CheckElement(row,  col,result, elementSetting);

                        if (bresult)
                            break;
                    }
                }
                else
                {
                    bool preCondition = CheckElement(row, col, result, condition.PreCnditions);

                    if (preCondition)
                    {
                        foreach (var elementSetting in condition.ElementSettings)
                        {
                            bresult = !CheckElement(row, col, result, elementSetting);

                            if (bresult)
                                break;
                        }
                    }
                }
            }

            return bresult;
        }
        /// <summary>
        /// ok true
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="result"></param>
        /// <param name="elementSetting"></param>
        /// <returns></returns>
        private static bool CheckElement(int row, int col, string result, ElementSetting elementSetting)
        {
            bool bresult  = false;
            bool position = true;

            if (elementSetting.Startindex > -1 && elementSetting.Length > 0)
            {
                if (elementSetting.Row >-1)
                {
                   position =  elementSetting.Row == row;
                }

                if (elementSetting.Col > -1)
                {
                    position = elementSetting.Col == col;
                }

                if (position)
                {
                    if ( elementSetting.Startindex + elementSetting.Length <= result.Length )
                    {
                        string checkValue = result.Substring(elementSetting.Startindex, elementSetting.Length);

                        if (elementSetting.Values.Contains(checkValue) == elementSetting.Contains)
                        {
                            bresult = true;
                        }
                        
                    }
                    else
                    {
                        bresult = true;
                    }

                }
                else
                {
                    //不检查 默认正确
                    return true;
                }

                if (bresult && elementSetting.ResultLength > 0)
                {
                    if (result.Length.Equals(elementSetting.ResultLength))
                    {
                        bresult = true;
                    }
                    else
                    {
                        bresult = false;
                    }
                }

                return bresult;
            }
            

            //if (elementSetting.Startindex == -1 && elementSetting.Length == 0
            //    && elementSetting.ResultLength > 0)
            if ( elementSetting.ResultLength > 0)
            {
                if (  result.Length.Equals(elementSetting.ResultLength))
                {
                    bresult = true;
                } 
            }

            return bresult;
        }


    }
}